package com.woniuxy.purchasemanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commons.entity.purchaseManager.NeedAllocationEntity;
import com.woniuxy.commons.entity.purchaseManager.NeedDemandGoodsEntity;
import com.woniuxy.commons.entity.purchaseManager.NeedEntity;
import com.woniuxy.commons.entity.purchaseManager.base.VoEntity;
import com.woniuxy.commons.entity.purchaseManager.needAllocationAndNeedDemandGoods;
import com.woniuxy.commons.utils.PageUtils;
import com.woniuxy.commons.utils.Query;
import com.woniuxy.purchasemanager.dao.NeedAllocationAndNeedDemandGoodsDao;
import com.woniuxy.purchasemanager.dao.NeedAllocationDao;
import com.woniuxy.purchasemanager.dao.NeedDemandGoodsDao;
import com.woniuxy.purchasemanager.service.NeedDemandGoodsService;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("needDemandGoodsService")
public class NeedDemandGoodsServiceImpl extends ServiceImpl<NeedDemandGoodsDao, NeedDemandGoodsEntity> implements NeedDemandGoodsService {

    @Resource
    private NeedAllocationDao needAllocationDao;

    @Resource
    private NeedAllocationAndNeedDemandGoodsDao needAllocationAndNeedDemandGoodsDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NeedDemandGoodsEntity> page = this.page(
                new Query<NeedDemandGoodsEntity>().getPage(params),
                new QueryWrapper<NeedDemandGoodsEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public Map<String, Object> pageListAll(Integer currentPage, Integer pageSize, String oddNumber) {
        Map<String, Object>  map = new HashMap<String, Object>();
        QueryWrapper<NeedAllocationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("odd_number",oddNumber);
        NeedAllocationEntity needAllocationEntity = needAllocationDao.selectOne(queryWrapper);
        map.put("NeedAllocationEntity",needAllocationEntity);
        Integer allocationDemandId = needAllocationEntity.getAllocationDemandId(); //得到ID
        //已获得当前的ID 跟据ID查询中间表
        QueryWrapper<needAllocationAndNeedDemandGoods> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("allocation_demand_id",allocationDemandId);
        List<needAllocationAndNeedDemandGoods> needAllocationAndNeedDemandGoods = needAllocationAndNeedDemandGoodsDao.selectList(queryWrapper1);

       List array= new ArrayList<>();
        for(int i = 0; i < needAllocationAndNeedDemandGoods.size();i++){
         QueryWrapper<NeedDemandGoodsEntity> queryWrapper2 = new QueryWrapper<>();
         queryWrapper2.eq("allocation_demand_id",needAllocationAndNeedDemandGoods.get(i).getNeedDemanedProductId());
            NeedDemandGoodsEntity needDemandGoodsEntity = baseMapper.selectOne(queryWrapper2);
            array.add(needDemandGoodsEntity);
        }

        map.put("needDemandGoodsEntity",array);
        return map;
    }




}